import { urlEncode } from '../../../utils/util.js'
import { Target, HttpUtil } from '../../../utils/http_util.js'
var config = require('../../../utils/config.js')
var utils = require('../../../utils/util.js')

// pages/index/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrlBase: config.ictx,

    mid:"1",
    minfo: { mid:1,px:30.5604132618, py:104.2705535889},
    costomerlocal:'',
    //页面项目选择信息
    service:false,
    serviceName:"",
    servicId:"",
    serviceTime:"",
    servicePrice:"",
    checked:"",
    serviseList: [],
    busdescribe:'', //项目描述

    currentTab:0,
    baseInfo:'',

    comments:[], //用户评论列表
    currentCount:1,  //用户分页
    isLastPage:false,


    ctype:'',
    cInfo:'',
    sc:false  //是否收藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取坐标信息
    wx.getStorage({
      key: 'locationInfo',
      success(lores) {
        that.setData({
          datalist: lores.data
        });
        options.latitude = lores.data.latitude;
        options.longitude = lores.data.longitude;
        HttpUtil.get(Target.self, "artificerMeans/searchDeatil", options).then(res => {
          console.log(res);
          that.setData({
            baseInfo:res,
            serviseList: res.business
            //comments: res.comments
          });
          for (var i = 0; i < res.business.length;i++){
            if (res.business[i].ced==1){
              that.setData({
                busdescribe:res.business[i].describe,
                service:true,
                serviceTime: res.business[i].serviceDuration,
                servicId: res.business[i].id,
                serviceName: res.business[i].businessName,
                servicePrice: res.business[i].price,
                checked: res.business[i]
              });
            }
          }
        })
      }
    })

    //评论加载
    HttpUtil.get(Target.self, "/consumerComment/searchListDto/" + options.id, { artificerId: options.id, pageCount: '10', currentCount: that.data.currentCount}).then(res => {
      that.setData({
        comments:res.list,
        isLastPage: res.isLastPage
      })
    });


    wx.getStorage({
      key: 'userType',
      success(typeinfo) {
        if(typeinfo.data=='0'){
          wx.getStorage({
            key: 'userInfo',
            success(userres) {
              that.setData({
                cInfo:userres.data,
                ctype: typeinfo.data
              });

              //查询收藏状态
              HttpUtil.get(Target.self, "consumerCollection/searchByConsumerIdArtificerId", { consumerId: userres.data.id, artificerId: options.id }).then(res => {
                if(res.data.length!=0){
                   that.setData({
                     sc:true
                   }) 
                }
              });
            }  
          });
        }else{
          that.setData({
            ctype: typeinfo.data
          })
        }  
      }
    });  


        

    

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  serviceCheck:function(event){
    var that =this;
    if(that.data.service){
      that.setData({
        service: false
      })
    }else{
      that.setData({
        service: true
      }) 
    }
  },
  checkThis:function(event){
    var that = this;
    var cs = event.currentTarget.dataset;
    var list = that.data.serviseList;
    var list2=[];
    var stime, sprice,checked,desc;
    for(var i=0;i<list.length;i++){
      var ts = list[i];
      if(cs.sid==list[i].id){
         ts.ced=1;
        stime = ts.serviceDuration;
        sprice=ts.price;
        desc = ts.describe
        checked=ts;
      }else{
        ts.ced=0;
      }
      list2.push(ts);
    }

    that.setData({
      servicId:cs.sid,
      serviceName:cs.sname,
      servicePrice:sprice,
      serviceTime:stime,
      serviseList:list2,
      busdescribe: desc,
      checked: checked
    })


  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  comfirmOder:function(){
    var that =this;
    if (that.data.servicId == "" || that.data.servicId==null){
      wx.showToast({
        title: '请选择服务项目！',
        duration: 2000,
        icon:"none"
      })
    }else{
      //let param = Object.assign(that.data.baseInfo, that.data.checked)
      wx.navigateTo({
        url: '/pages/order/order-create/order?baseinfo=' + JSON.stringify(that.data.baseInfo) + '&ser=' + JSON.stringify(that.data.checked) + '&cinfo=' + JSON.stringify(that.data.cInfo),
      })
    }
  },
  mapDail:function(){
    var that = this;
    wx.navigateTo({
      url: '/pages/pulic/map/map?latitude=' + that.data.baseInfo.latitude + '&longitude=' + that.data.baseInfo.longitude
    })
  },
  collectionArt:function(){
    var that = this;
    //查询收藏状态
    HttpUtil.get(Target.self, "consumerCollection/add", { status: 1, customerId: that.data.cInfo.id, artificerId: that.data.baseInfo.id }).then(res => {
      if(res.code==200){
        that.setData({
          sc:true
        })
        wx.showToast({
          title: '收藏成功！',
          duration: 2000,
          icon: "none"
        })
      }
    });
  },
  unCollectionArt: function () {
    var that = this;
    //查询收藏状态
    HttpUtil.get(Target.self, "consumerCollection/deleteCollection", { consumerId: that.data.cInfo.id, artificerId: that.data.baseInfo.id }).then(res => {
      that.setData({
        sc: false
      })
      wx.showToast({
        title: '已取消收藏！',
        duration: 2000,
        icon: "none"
      })
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.currentTab==3){
      if (that.data.isLastPage==true){
        // wx.showToast({
        //   title: '已加载全部评论！',
        //   duration: 2000,
        //   icon: "none"
        // })
        return;
      }  
      wx.showLoading({
        title: '加载中..',
      });

      //评论加载
      HttpUtil.get(Target.self, "/consumerComment/searchListDto/" + that.data.baseInfo.id, { artificerId: that.data.baseInfo.id, pageCount: '10', currentCount: that.data.currentCount }).then(res => {
        var thatcomm = that.data.comments;
        thatcomm.push(res.data);
        that.setData({
          comments: thatcomm,
          isLastPage: res.isLastPage
        })

        setTimeout(function () {
          wx.hideLoading()
        }, 1000);
      });

      
    }
  },
  imgdetail: function (event){
    var url = event.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })  
  }
})